package com.wanjia.bpsapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanjia.bpsapi.annotation.LoginToken;
import com.wanjia.bpsapi.pojo.dto.VegeParamTable;
import com.wanjia.bpsapi.pojo.dto.VegeParamUpdate;
import com.wanjia.bpsapi.pojo.model.JsonResult;
import com.wanjia.bpsapi.service.IVegeParamService;
import com.wanjia.bpsapi.util.JsonResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/conversion")
@Tag(name = "成本换算接口控制器")
public class ConversionController extends BaseController {

    @Setter(onMethod_ = @Autowired)
    private IVegeParamService service;

    @GetMapping
    @Operation(summary = "换算表查询接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Page<VegeParamTable>> pageVegeParam(
            @Parameter(description = "菜品id") @RequestParam(required = false) Long vegeId,
            @Parameter(description = "成本项id") @RequestParam(required = false) Long paramId,
            @Parameter(description = "页码") Integer pageNo,
            @Parameter(description = "页尺寸") Integer pageSize
    ) {
        return JsonResultUtil.success(service.pageVegeParam(vegeId, paramId, pageNo - 1, pageSize));
    }

    @PutMapping
    @Operation(summary = "换算表更新接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Void> updateVegeParam(@RequestBody List<VegeParamUpdate> updates) {
        service.updateVegeParam(updates);
        return JsonResultUtil.success();
    }

    @PostMapping("/fix")
    @Operation(summary = "换算表数据修复接口")
    @Parameter(name = "token", in = ParameterIn.HEADER)
    @LoginToken
    public JsonResult<Void> fixData() {
        service.fixData();
        return JsonResultUtil.success();
    }
}
